package com.shioirikukuri.mapper;

import com.shioirikukuri.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Optional;

@Mapper
public interface UserMapper {

	@Insert("INSERT INTO users(phone_number, password, full_name, enabled, role, avatar_url) " +
			"VALUES(#{phoneNumber}, #{password}, #{fullName}, #{enabled}, #{role}, #{avatarUrl})")
	void addUser(User user);

	@Delete("DELETE FROM users WHERE id = #{id}")
	void deleteUser(Long id);

	@Update("UPDATE users SET phone_number=#{phoneNumber}, password=#{password}, full_name=#{fullName}, enabled=#{enabled}, role=#{role}, avatar_url=#{avatarUrl} WHERE id=#{id}")
	void updateUser(User user);

	@Select("SELECT * FROM users WHERE phone_number = #{phoneNumber}")
	Optional<User> findUserByPhoneNumber(String phoneNumber);

	@Select("SELECT * FROM users")
	List<User> getAllUsers();

	@Select("SELECT * FROM users WHERE id = #{id}")
	Optional<User> getUserById(Long id);

	@Update("UPDATE users SET enabled = true WHERE id = #{id}")
	void blacklistUser(Long id);

	@Update("UPDATE users SET enabled = false WHERE id = #{id}")
	void unblacklistUser(Long id);


	@Select("SELECT * FROM users WHERE role = #{role}")
	List<User> getUsersByRole(String role);

	@Select("SELECT * FROM users WHERE role = 'consumer'")
	List<User> getAllConsumers();

	/**
	 * 查看用户是否被拉黑，检查enabled字段属性是否为ENABLED
	 * @param phoneNumber
	 * @return
	 */
	@Select("SELECT enabled FROM users WHERE phone_number = #{phoneNumber}")
	Boolean isUserEnabled(String phoneNumber);

}